import 'dart:convert';
import 'package:http/http.dart' as http;

const String apiKey = 'bcScUK7YSZiUFDf9'; // 请替换为你的真实API Key

class WeatherService {
  static const String baseUrl = 'https://api.caiyunapp.com/v2.6';

  // 获取实况天气
  static Future<Map<String, dynamic>?> fetchRealtimeWeather(double lng, double lat) async {
    final url = '$baseUrl/$apiKey/$lng,$lat/realtime';
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      return json.decode(response.body);
    }
    return null;
  }

  // 获取未来3天天气
  static Future<Map<String, dynamic>?> fetchDailyWeather(double lng, double lat) async {
    final url = '$baseUrl/$apiKey/$lng,$lat/daily?dailysteps=3';
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      return json.decode(response.body);
    }
    return null;
  }

  // 综合获取实况和未来3天天气
  static Future<Map<String, dynamic>?> fetchWeather(double lng, double lat) async {
    final realtime = await fetchRealtimeWeather(lng, lat);
    final daily = await fetchDailyWeather(lng, lat);
    if (realtime != null && daily != null) {
      return {
        'realtime': realtime['result']['realtime'],
        'daily': daily['result']['daily'],
      };
    }
    return null;
  }
}